package one.stand.service.impl;

import com.jmp.autocofiguration.util.OssUtil2;
import com.jmp.base.dto.SunCodeCardRequest;
import com.jmp.base.dto.SunCodeCardResponse;
import com.jmp.feign.SunCodeFeign;
import one.stand.advice.ResultEnum;
import one.stand.code.RedisCode;
import one.stand.config.CardPosterConfig;
import one.stand.entity.PosterEntity;
import one.stand.model.BusinessCard;
import one.stand.model.Company;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CardFacadeService;
import one.stand.service.CompanyBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.ImageGenerateUtil;
import one.stand.util.OneListUtil;
import one.stand.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author cjr
 */
@Service
public class CardFacadeServiceImpl implements CardFacadeService {
    @Autowired
    private SunCodeFeign sunCodeFeign;

    @Autowired
    private CardPosterConfig cardPosterConfig;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ResultModel getCardPoster(BusinessCardRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        List<String> list = redisUtil.get(RedisCode.getCardPoster(request.getCardId()));
        if (OneListUtil.isNotEmpty(list)) {
            return ResultModel.success(list);
        }

        SunCodeCardRequest sunCodeCardRequest = SunCodeCardRequest.builder().cardId(request.getCardId()).build();
        ResultModel<SunCodeCardResponse> sunCodeCardResponseResultModel = sunCodeFeign.card(sunCodeCardRequest);
        AssertUtil.isTrue(sunCodeCardResponseResultModel);
        String wxTwoCode = sunCodeCardResponseResultModel.getData().getCodeUrl();
        AssertUtil.nonNull(wxTwoCode, ResultEnum.FAIL);
        BusinessCard model = businessCardBaseService.getCustomer(request.getCardId());
        Company company = companyBaseService.getById(model.getCompanyId());
        AssertUtil.nonNull(model, ResultEnum.PARAM_CHECK);

        list = new ArrayList<>();
        list.add(card1(model, company, wxTwoCode));
        list.add(card2(model, company, wxTwoCode));
        list.add(card3(model, wxTwoCode));

        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
        redisUtil.set(RedisCode.getCardPoster(request.getCardId()), list, seconds, TimeUnit.SECONDS);

        return ResultModel.success(list);
    }

    private String card1(BusinessCard model, Company company, String wxTwoCode) {
        PosterEntity posterEntity = new PosterEntity();
        posterEntity.setImagePath(cardPosterConfig.getUrl1());
        posterEntity.setCompanyName(getObjectString(company.getCompanyName()));
        posterEntity.setEmail(getObjectString(model.getEmail()));
        posterEntity.setPhone(getObjectString(model.getPhone()));
        posterEntity.setCardName(getObjectString(model.getName()));
        posterEntity.setQRCodeUrl(getObjectString(wxTwoCode));
        posterEntity.setCardLogoUrl(getObjectString(model.getLogo()));
        posterEntity.setAppName(null);
        InputStream inputStream = ImageGenerateUtil.businessCardDraw1(posterEntity);
        return OssUtil2.upload(inputStream, OssUtil2.FileSuffix.png);
    }

    private String card2(BusinessCard model, Company company, String wxTwoCode) {
        PosterEntity posterEntity = new PosterEntity();
        posterEntity.setCardName(getObjectString(model.getName()));
        posterEntity.setPosition(getObjectString(model.getPosition()));
        posterEntity.setPhone(getObjectString(model.getPhone()));
        posterEntity.setQRCodeUrl(getObjectString(wxTwoCode));
        posterEntity.setCardLogoUrl(StringUtils.isBlank(model.getLogo()) ? cardPosterConfig.getCardLogoDefaultUrl() : model.getLogo());
        posterEntity.setCompanyLogo(getObjectString(company.getCompanyLogo()));
        posterEntity.setAppName(null);
        posterEntity.setImagePath(cardPosterConfig.getUrl2());
        posterEntity.setCard2BackUrl(cardPosterConfig.getUrl2back());
        InputStream inputStream = ImageGenerateUtil.businessCardDraw2(posterEntity);
        return OssUtil2.upload(inputStream, OssUtil2.FileSuffix.png);
    }

    private String card3(BusinessCard model, String wxTwoCode) {
        PosterEntity posterEntity = new PosterEntity();
        posterEntity.setQRCodeUrl(getObjectString(wxTwoCode));
        posterEntity.setCardLogoUrl(StringUtils.isBlank(model.getLogo()) ? cardPosterConfig.getCardLogoDefaultUrl() : model.getLogo());
        posterEntity.setCardName(getObjectString(model.getName()));
        posterEntity.setEmail(getObjectString(model.getEmail()));
        posterEntity.setImagePath(cardPosterConfig.getUrl3());
        posterEntity.setPhone(getObjectString(model.getPhone()));
        posterEntity.setAppName(null);
        InputStream inputStream = ImageGenerateUtil.businessCardDraw3(posterEntity);
        return OssUtil2.upload(inputStream, OssUtil2.FileSuffix.png);
    }

    private static String getObjectString(Object obj) {
        if (obj == null || StringUtils.isBlank(obj + "")) {
            return " ";
        }
        return obj.toString();
    }
}
